//
//  AlertView.m
//  iOSTools
//
//  Created by BaizYi on 2018/12/28.
//  Copyright © 2018 YJY. All rights reserved.
//

#import "AlertView.h"

static const CGFloat alertViewMargin      = 30;
static const CGFloat textFieldMargin      = 10;
static const CGFloat textFieldH           = 40;
static const CGFloat toolBarH            = 40.5;

@interface AlertView ()

@property (nonatomic, strong) UILabel *titleLB;
@property (nonatomic, strong) UILabel *messageLB;
@property (nonatomic, strong) UIView  *toolBar;
@property (nonatomic, strong) UIView  *alertView;
@property (nonatomic, assign) CGFloat alertViewWidth;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) UIView   *alertViewS;

@end

@implementation AlertView

+(void)showAlertWithMessage:(NSString *)message toView:(UIView *)view{
    AlertView *alertView = [[AlertView alloc]initWithMessage:message];
    [alertView showInView:view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alertView dismissAnimation];
    });
}

-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message alertViewBlock:(AlertViewBlock)block{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:106/255.00f green:106/255.00f blue:106/255.00f alpha:0.4];
        self.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
        self.title = title;
        self.message = message;
        self.alertViewBlock = block;
        self.alertViewWidth = Screen_Width - 2*alertViewMargin;
        [self addSubview:self.alertView];
        [self showAnimation];
    }
    return self;
}

-(instancetype)initWithMessage:(NSString *)message{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
        self.message = message;
        self.alertViewWidth = Screen_Width - 2*alertViewMargin;
        [self addSubview:self.alertViewS];
        [self showAnimation];
        
    }
    return self;
}

-(void)showInView{
    [self showInView:nil];
}

-(void)showInView:(UIView *)view{
    if (view) {
        [view addSubview:self];
    }else{
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self];
    }
}

-(void)sureButtonDidClicked{
    if (self.alertViewBlock) {
        self.alertViewBlock(YES);
    }
    [self dismissAnimation];
}
-(void)cancelButtonDidClicked{
    if (self.alertViewBlock) {
        self.alertViewBlock(NO);
    }
    [self dismissAnimation];
    
}
-(void)showAnimation{
    
    self.alertView.transform = CGAffineTransformMakeScale(0.2, 0.2);
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)dismissAnimation{
    [UIView animateWithDuration:0.25 animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(0.2, 0.2);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)setTitle:(NSString *)title{
    _title = title;
}
-(void)setMessage:(NSString *)message{
    _message = message;
}

-(UILabel *)titleLB{
    if (_titleLB == nil) {
        _titleLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.alertViewWidth, 20)];
        _titleLB.text = self.title;
        _titleLB.tintColor = [UIColor darkTextColor];
        _titleLB.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLB;
}

-(UILabel *)messageLB{
    if (_messageLB == nil) {
        CGFloat originY = CGRectGetMaxY(self.titleLB.frame);
        if (self.title == nil || self.title.length == 0) {
            originY = 20;
        }
        _messageLB = [[UILabel alloc]initWithFrame:CGRectMake(0, originY, self.alertViewWidth, 60)];
        _messageLB.text = self.message;
        _messageLB.textColor = [UIColor darkTextColor];
        _messageLB.numberOfLines = 0;
        _messageLB.font = [UIFont systemFontOfSize:15];
        _messageLB.textAlignment = NSTextAlignmentCenter;
        
        
    }
    return _messageLB;
}

-(UIView *)toolBar{
    if (_toolBar == nil) {
        CGFloat buttonW = (self.alertViewWidth-0.5)/2;
        _toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.messageLB.frame)+2*textFieldMargin, self.alertViewWidth, toolBarH)];
        _toolBar.backgroundColor = [UIColor whiteColor];
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(buttonW+0.5, 1, buttonW, toolBarH-1)];
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        sureBtn.backgroundColor = [UIColor whiteColor];
        [sureBtn addTarget:self action:@selector(sureButtonDidClicked) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 1, buttonW, toolBarH-1)];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        cancelBtn.backgroundColor = [UIColor whiteColor];
        [cancelBtn addTarget:self action:@selector(cancelButtonDidClicked) forControlEvents:UIControlEventTouchUpInside];
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_toolBar.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = _toolBar.bounds;
        shapeLayer.path = path.CGPath;
        shapeLayer.fillColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [_toolBar.layer addSublayer:shapeLayer];
        [_toolBar.layer setMasksToBounds:YES];
        
        [_toolBar addSubview:sureBtn];
        [_toolBar addSubview:cancelBtn];
    }
    return _toolBar;
}

-(UIView *)alertView{
    if (_alertView == nil) {
        CGRect frame = CGRectMake(0, 0, self.alertViewWidth, CGRectGetHeight(self.titleLB.frame)+CGRectGetHeight(self.messageLB.frame)+25);
        _alertView = [[UIView alloc]initWithFrame:frame];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.layer.cornerRadius = 5;
        if (self.title != nil && self.title.length != 0) {
            frame = CGRectMake(0, 0, self.alertViewWidth, CGRectGetHeight(self.titleLB.frame)+CGRectGetHeight(self.messageLB.frame)+CGRectGetHeight(self.toolBar.frame)+ 2*textFieldMargin + 20);
            [_alertView addSubview:self.titleLB];
        }
        _alertView.frame = frame;
        [_alertView addSubview:self.messageLB];
        _alertView.center = self.center;
        [_alertView addSubview:self.toolBar];
        [_alertView.layer setMasksToBounds:YES];
    }
    return _alertView;
    
}
-(UIView *)alertViewS{
    if (_alertViewS == nil) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.alertViewWidth, 60)];
        _alertView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        _alertView.layer.cornerRadius = 5;
        _alertView.center = self.center;
        self.messageLB.frame = CGRectMake(30, 0, CGRectGetWidth(_alertView.frame)-60, CGRectGetHeight(_alertView.frame));
        self.messageLB.textColor = [UIColor whiteColor];
        [_alertView addSubview:self.messageLB];
        
    }
    return _alertView;
}


@end
